package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baor.dao.*;
import com.baor.dao.TagFieldDao;
import com.baor.domain.GoodsVo;
import com.baor.domain.LogoVo;
import com.baor.domain.SeparateVo;
import com.baor.domain.SpecificationVo;
import com.baor.domain.field.TagField;
import com.baor.service.GoodsService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baor.utils.DateUtil.now;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogoDao logoDao;
    @Autowired
    private TagFieldDao tagFieldDao;
    @Autowired
    private SeparateDao separateDao;
    @Autowired
    private SpecificationDao specificationDao;
    @Override
    public AjaxResult<?> goodsList(Map<String, Object> params) {
        params.put("pageIndex", (int)params.get("pageSize") * ((int)(params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("goodsList", goodsDao.goodsList(params));
        res.put("pageCount",goodsDao.pageCount(params));
        QueryWrapper<LogoVo> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", 0).eq("site_id", params.get("siteId"));
        res.put("logo",logoDao.selectList(wrapper));
        QueryWrapper<SeparateVo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("delflag", 0).eq("site_id", params.get("siteId"));
        res.put("separate",separateDao.selectList(wrapper2));
        QueryWrapper<TagField> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("delflag", 0);
        res.put("tag",tagFieldDao.selectList(wrapper3));
        List<String> list = new ArrayList<>();
        list.add("实物商品");
        list.add("虚拟商品");
        res.put("goodsType",list);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> goodsInfo(GoodsVo goodsVo) {
        GoodsVo goods = goodsDao.selectById(goodsVo.getGoodsId());
        goods.setTagIds(goodsDao.goodsTag(goodsVo.getGoodsId()));
        goods.setSpecification(specificationDao.selectList(new QueryWrapper<SpecificationVo>().eq("goods_id", goodsVo.getGoodsId())));
        Map<String,Object> res = new HashMap<>();
        res.put("goodsInfo", goods);
        QueryWrapper<LogoVo> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", 0).eq("site_id", goodsVo.getSiteId());
        res.put("logo",logoDao.selectList(wrapper));
        QueryWrapper<SeparateVo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("delflag", 0).eq("site_id", goodsVo.getSiteId());
        res.put("separate",separateDao.selectList(wrapper2));
        QueryWrapper<TagField> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("delflag", 0);
        res.put("tag",tagFieldDao.selectList(wrapper3));
        List<String> list = new ArrayList<>();
        list.add("实物商品");
        list.add("虚拟商品");
        res.put("goodsType",list);
        return AjaxResult.success(res);
    }

    @Override
    @Transactional
    public AjaxResult<?> insertGoods(GoodsVo goodsVo) {
        LambdaQueryWrapper<GoodsVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsVo::getDelFlag,0);
        List<GoodsVo> list = goodsDao.selectList(queryWrapper);
        if(!(goodsVo.getPriceCode().equals(""))) {
            long count = list.stream().filter(x -> x.getPriceCode().equals(goodsVo.getPriceCode())).count();
            if (count > 0) {
                return AjaxResult.error("商品码重复");
            }
        }
        goodsVo.setCreateTime(now());
        int rows = goodsDao.insert(goodsVo);
        Integer id = goodsVo.getGoodsId();
        System.out.println(id+"-----------------");
        if(rows > 0){
            for(Integer tagId : goodsVo.getTagIds()){
                Map<String,Object> params = new HashMap<>();
                params.put("goodsId", goodsVo.getGoodsId());
                params.put("tagId", tagId);
                goodsDao.insertGoodsTag(params);
            }
            if(goodsVo.getSpecificationState() == 1){
                for(SpecificationVo specificationVo : goodsVo.getSpecification()){
                    specificationVo.setGoodsId(id);
                    specificationDao.insert(specificationVo);
                }
            }
            return AjaxResult.success("添加成功");
        }else{
            return AjaxResult.error("添加失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult<?> updateGoods(GoodsVo goodsVo) {
        int rows = goodsDao.updateById(goodsVo);
        if(rows > 0){
            if(goodsVo.getTagIds()!= null && goodsVo.getTagIds().size() > 0){
                goodsDao.deleteGoodsTag(goodsVo.getGoodsId());
                if(goodsVo.getSpecificationState() == 1){
                    for(SpecificationVo specificationVo : goodsVo.getSpecification()){
                        specificationDao.updateById(specificationVo);
                    }
                }
                for(Integer tagId : goodsVo.getTagIds()){
                    Map<String,Object> params = new HashMap<>();
                    params.put("goodsId", goodsVo.getGoodsId());
                    params.put("tagId", tagId);
                    goodsDao.insertGoodsTag(params);
                }
            }
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }

    @Override
    @Transactional
    public AjaxResult<?> deleteGoods(List<GoodsVo> goodsVo) {
        for(GoodsVo g : goodsVo){
            g.setDelFlag(1);
            goodsDao.updateById(g);
            goodsDao.deleteGoodsTag(g.getGoodsId());
        }
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult<?> recycleList(Map<String, Object> params) {
        params.put("pageIndex", (int)params.get("pageSize") * ((int)(params.get("pageIndex")) - 1));
        params.put("pageSize", params.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("goodsList", goodsDao.recycleList(params));
        res.put("pageCount",goodsDao.recycleCount(params));
        QueryWrapper<SeparateVo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("delflag", 0).eq("site_id", params.get("siteId"));
        res.put("separate",separateDao.selectList(wrapper2));
        List<String> list = new ArrayList<>();
        list.add("实物商品");
        list.add("虚拟商品");
        res.put("goodsType",list);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> restoreGoods(List<GoodsVo> goodsVo) {
        for(GoodsVo g : goodsVo){
            g.setDelFlag(0);
            goodsDao.updateById(g);
            goodsDao.restoreGoodsTag(g.getGoodsId());
        }
        return AjaxResult.success("还原成功");
    }

    @Override
    public AjaxResult<?> unShelve(List<GoodsVo> goodsVo) {
        for(GoodsVo g : goodsVo){
            g.setGoodsSell("下架");
            g.setGoodsState("仓库中");
            goodsDao.updateById(g);
        }
        return AjaxResult.success("下架成功");
    }

    @Override
    public AjaxResult<?> Shelve(List<GoodsVo> goodsVo) {
        for(GoodsVo g : goodsVo){
            g.setGoodsSell("上架");
            g.setGoodsState("销售中");
            goodsDao.updateById(g);
        }
        return AjaxResult.success("上架成功");
    }


}
